# src/files/kno_type.py
from flask import request, jsonify
from src.pojo.kno_type import KnoType
from database.database import db

def register_kno_type_routes(app):
    # 添加知识类型
    @app.route('/kno_type', methods=['POST'])
    def add_kno_type():
        data = request.get_json()
        knokinds_id = data.get('knokinds_id')
        kno_type = data.get('kno_type')

        if not knokinds_id or not kno_type:
            return jsonify({'code': 401, 'message': '缺少必要参数'}), 401

        # 检查知识分类ID是否已存在
        existing_kno_type_id = KnoType.query.filter_by(knokinds_id=knokinds_id).first()
        if existing_kno_type_id:
            return jsonify({'code': 402, 'message': '知识分类ID已存在'}), 402
        
        # 检查知识分类名称是否已存在
        existing_kno_type_name = KnoType.query.filter_by(kno_type=kno_type).first()
        if existing_kno_type_name:
            return jsonify({'code': 403, 'message': '知识分类名称已存在'}), 403

        new_kno_type = KnoType(
            knokinds_id=knokinds_id,
            kno_type=kno_type
        )

        try:
            db.session.add(new_kno_type)
            db.session.commit()
            return jsonify({'code': 200, 'message': '知识类型创建成功', 'data': new_kno_type.to_dict()}), 200
        except Exception as e:
            db.session.rollback()
            return jsonify({'code': 500, 'message': '创建知识类型失败: ' + str(e)}), 500

    # 查询所有知识类型
    @app.route('/kno_types', methods=['GET'])
    def get_kno_types():
        try:
            kno_types = KnoType.query.all()
            kno_types_list = [kno_type.to_dict() for kno_type in kno_types]
            return jsonify({'code': 200, 'message': '查询成功', 'data': kno_types_list}), 200
        except Exception as e:
            return jsonify({'code': 500, 'message': '查询失败: ' + str(e)}), 500

    # 根据ID查询知识类型
    @app.route('/kno_type/<id>', methods=['GET'])
    def get_kno_type(id):
        try:
            kno_type = KnoType.query.filter_by(id=id).first()
            if kno_type:
                return jsonify({'code': 200, 'message': '查询成功', 'data': kno_type.to_dict()}), 200
            else:
                return jsonify({'code': 404, 'message': '知识类型不存在'}), 404
        except Exception as e:
            return jsonify({'code': 500, 'message': '查询失败: ' + str(e)}), 500

    # 更新知识类型
    @app.route('/kno_type/<id>', methods=['PUT'])
    def update_kno_type(id):
        data = request.get_json()
        knokinds_id = data.get('knokinds_id')
        kno_type = data.get('kno_type')

        if not knokinds_id or not kno_type:
            return jsonify({'code': 401, 'message': '缺少必要参数'}), 401

        kno_type_obj = KnoType.query.filter_by(id=id).first()
        if not kno_type_obj:
            return jsonify({'code': 404, 'message': '知识类型不存在'}), 404

        kno_type_obj.knokinds_id = knokinds_id
        kno_type_obj.kno_type = kno_type

        try:
            db.session.commit()
            return jsonify({'code': 200, 'message': '知识类型更新成功', 'data': kno_type_obj.to_dict()}), 200
        except Exception as e:
            db.session.rollback()
            return jsonify({'code': 500, 'message': '更新知识类型失败: ' + str(e)}), 500

    # 删除知识类型
    @app.route('/kno_type/<id>', methods=['DELETE'])
    def delete_kno_type(id):
        try:
            kno_type_obj = KnoType.query.filter_by(id=id).first()
            if not kno_type_obj:
                return jsonify({'code': 404, 'message': '知识类型不存在'}), 404

            db.session.delete(kno_type_obj)
            db.session.commit()
            return jsonify({'code': 200, 'message': '知识类型删除成功'}), 200
        except Exception as e:
            db.session.rollback()
            return jsonify({'code': 500, 'message': '删除知识类型失败: ' + str(e)}), 500
